package models

import (
	"fmt"
	"gitee.com/gitee-go/core/common"
	"gitee.com/gitee-go/core/model/pipeline"
	"regexp"
	"time"
)

type ShowPipelineVersion struct {
	Id                  string `xorm:"not null pk VARCHAR(64)" json:"id"`
	Trigger             string `xorm:"comment('触发方式') VARCHAR(100)" json:"trigger"`
	Events              string `xorm:"comment('事件push、pr、note') VARCHAR(100)" json:"events"`
	Ref                 string `xorm:"VARCHAR(255)" json:"ref"`
	Branch              string `xorm:"VARCHAR(255)" json:"branch"`
	RepoId              string `xorm:"VARCHAR(64)" json:"repoId"`
	RepoName            string `xorm:"VARCHAR(255)" json:"repoName"`
	CommitSha           string `xorm:"VARCHAR(255)" json:"commitSha"`
	CommitMessage       string `xorm:"comment('提交信息') TEXT" json:"commitMessage"`
	PipelineName        string `xorm:"VARCHAR(255)" json:"pipelineName"`
	PipelineDisplayName string `xorm:"VARCHAR(255)" json:"pipelineDisplayName"`
	PipelineId          string `xorm:"VARCHAR(64)" json:"pipelineId"`
	Version             string `xorm:"VARCHAR(255)" json:"version"`
	//YmlContent          string    `xorm:"LONGTEXT" json:"ymlContent"`
	//JsonContent         string    `xorm:"LONGTEXT" json:"jsonContent"`
	CreateTime         time.Time `xorm:"DATETIME" json:"createTime"`
	CreateUser         string    `xorm:"VARCHAR(255)" json:"createUser"`
	CreateUserId       string    `xorm:"VARCHAR(64)" json:"createUserId"`
	TargetRepoName     string    `xorm:"VARCHAR(255)" json:"targetRepoName"`
	TargetRepoSha      string    `xorm:"VARCHAR(255)" json:"targetRepoSha"`
	TargetRepoRef      string    `xorm:"VARCHAR(255)" json:"targetRepoRef"`
	TargetRepoCloneUrl string    `xorm:"VARCHAR(255)" json:"targetRepoCloneUrl"`
	Title              string    `xorm:"VARCHAR(255)" json:"prName"`
	PrNumber           int64     `xorm:"BIGINT(20)" json:"-"`

	//build
	Status     string    `xorm:"comment('构建状态') VARCHAR(100)" json:"status"`
	Error      string    `xorm:"comment('错误信息') VARCHAR(500)" json:"error"`
	Number     int64     `xorm:"comment('构建次数') BIGINT(20)" json:"number"`
	Created    time.Time `xorm:"comment('创建时间') DATETIME" json:"-"`
	Started    time.Time `xorm:"comment('开始时间') DATETIME" json:"started"`
	Finished   time.Time `xorm:"comment('结束时间') DATETIME" json:"finished"`
	ExpendTime int64     `xorm:"-" json:"expendTime"`

	UrlBranch    string `xorm:"-" json:"urlBranch"`
	UrlCommitSha string `xorm:"-" json:"urlCommitSha"`
	UrlPr        string `xorm:"-" json:"urlPr"`

	TpvStatus   string `xorm:"VARCHAR(255)" json:"versionStatus"`
	BuildStatus string `xorm:"VARCHAR(255)" json:"buildStatus"`
	TpvError    string `xorm:"VARCHAR(255)" json:"-"`

	VersionMsg string `xorm:"-" json:"versionMsg"`
}

var RegGit = regexp.MustCompile(`(.*)+(\.git)$`)

func (ShowPipelineVersion) TableName() string {
	return "t_pipeline_version"
}
func (c *ShowPipelineVersion) CompletionFieldValue(repo *pipeline.TRepo) {
	c.ExpendTime = int64(c.Finished.Sub(c.Started).Seconds())
	if c.Finished.IsZero() && !c.Started.IsZero() {
		c.ExpendTime = int64(time.Since(c.Started).Seconds())
	}
	if repo.RepoType == common.GITEE {
		if RegGit.MatchString(repo.Url) {
			s := RegGit.FindStringSubmatch(repo.Url)
			repo.Url = s[1]
		}
	}
	c.UrlBranch = fmt.Sprintf("%v/tree/%v", repo.Url, c.Branch)
	c.UrlCommitSha = fmt.Sprintf("%v/commit/%v", repo.Url, c.CommitSha)
	if c.Events == common.EVENTS_TYPE_PR ||
		c.Events == common.GITEE_EVENT_PR_ACTION_UPDATE ||
		c.Events == common.EVENTS_TYPE_COMMENT {

		c.UrlPr = fmt.Sprintf("%v/pulls/%v", repo.Url, c.PrNumber)
		c.UrlBranch = fmt.Sprintf("%v/tree/%v", c.TargetRepoCloneUrl, c.TargetRepoRef)
		c.UrlCommitSha = fmt.Sprintf("%v/commit/%v", repo.Url, c.CommitSha)

	}
	c.Status = c.BuildStatus
	if c.TpvStatus != common.PIPELINE_VERSION_STATUS_OK {
		c.Status = c.TpvStatus
	}

	if c.TpvStatus == common.PIPELINE_VERSION_STATUS_ERROR {
		c.VersionMsg = c.TpvError
	}
	if c.TpvStatus == common.PIPELINE_VERSION_STATUS_PENDING {
		c.VersionMsg = "yaml文件等待解析"
	}
	if c.TpvStatus == common.PIPELINE_VERSION_STATUS_RUNNING {
		c.VersionMsg = "yaml文件正在解析中"
	}
}
